/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.mobiubiq.directory.client.widget;

import com.google.zxing.Result;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Vector;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;
import javax.microedition.media.Manager;
import javax.microedition.media.MediaException;
import javax.microedition.media.Player;
import javax.microedition.media.control.VideoControl;
import org.kalmeo.kuix.core.Kuix;
import org.kalmeo.kuix.widget.ActionWidget;
import org.mobiubiq.directory.client.object.BarcodeObject;

/**
 *
 * @author Marnix Renne
 */
public class BarcodeReaderWidget extends ActionWidget {

    public static final String BARCODE_READER_WIDGET_TAG = "barcodereader";
    private Canvas canvas;
    private Player player;
    private VideoControl videoControl;
    private Vector listeners;
    private SnapshotThread snapshotThread;
    
    public BarcodeReaderWidget() {
        super(BARCODE_READER_WIDGET_TAG);
        listeners = new Vector();
        try {
            player = createPlayer();
            player.realize();
            AdvancedMultimediaManager.setZoom(player);
            AdvancedMultimediaManager.setExposure(player);
        } catch (IOException ex) {
            Kuix.alert(ex);
        } catch (MediaException ex) {
            Kuix.alert(ex);
        }
    }

    Player getPlayer() {
        return player;
    }

    VideoControl getVideoControl() {
        return videoControl;
    }

    void stop() {

    }

    void handleDecodedText(Result result) {
        BarcodeObject object = new BarcodeObject(result);
        Enumeration e = listeners.elements();
        while(e.hasMoreElements()) {
            BarcodeListener l = (BarcodeListener) e.nextElement();
            l.barcodeDetected(object);
        }
    }

    private Player createPlayer() throws IOException, MediaException {
        // Try a workaround for Nokias, which want to use capture://image in some cases
        Player player = null;
        String platform = System.getProperty("microedition.platform");
        if (platform != null && platform.indexOf("Nokia") >= 0) {
            try {
                player = Manager.createPlayer("capture://image");
            } catch (MediaException me) {
            //    Kuix.alert(me);
            // if this fails, just continue with capture://video
            } catch (Error e) {
            //    Kuix.alert(e);
            // Ugly, but, it seems the Nokia N70 throws "java.lang.Error: 136" here
            // We should still try to continue
            }
        }
        if (player == null) {
            player = Manager.createPlayer("capture://video");
        }
        return player;
    }

    public void paint(Graphics g) {
        super.paint(g);
        g.setColor(0x000000);
        g.fillRect(0, 0, getWidth()-1, getHeight()-1);
        try {
            if (videoControl == null) {
                videoControl = (VideoControl) player.getControl("VideoControl");
                canvas = Kuix.getCanvas();
                videoControl.initDisplayMode(VideoControl.USE_DIRECT_VIDEO, canvas);
                videoControl.setDisplayLocation(getDisplayX(), getDisplayY());
                videoControl.setDisplaySize(getWidth(), getHeight());
                videoControl.setVisible(true);
                player.start();
                snapshotThread = new SnapshotThread(this);
                new Thread(snapshotThread).start();
            }
        } catch (MediaException ex) {
            Kuix.alert(ex);
        }
    }

    public void cleanUp() {
        super.cleanUp();
        if (player != null) {
            try {
                player.stop();
            } catch (MediaException me) {
            // continue
            }
            player.deallocate();
            player.close();
            player = null;
            videoControl = null;
        }
    }
    
    public void addBarcodeListener(BarcodeListener listener) {
        listeners.addElement(listener);
    }

   public void takeSnapshot() {
       snapshotThread.continueRun();
    }
}
